package cn.highedu.gatewaydemo.demo;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@Component
@RefreshScope
@RestController
public class DemoBean {

    Logger logger = org.slf4j.LoggerFactory.getLogger(DemoBean.class);

    @Value("${message}")
    private String message;

    @PostConstruct
    public void init() throws NacosException {
        logger.info("从配置文件中读取到的message值是: {}", message);

        ConfigService configService = NacosFactory.createConfigService(
                "localhost:8848");
        String content = configService.getConfig(
                "gateway-prod.yaml", "DEFAULT_GROUP", 3000);
        logger.info("从Nacos中读取到的配置内容是: {}", content);
    }

    @GetMapping("/hello")
    public String hello() {
        return message;
    }
}
